package service

import (
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/redisUtil"
	"MyBlogv2/blog-common/response"
	"github.com/gin-gonic/gin"
	"strconv"
)

func GetRoleHasPermission(c *gin.Context) {
	permissionId, _ := strconv.Atoi(c.Query("permissionId"))
	rollAllVoList := selectRoleByPermissionId(permissionId, "", "", 0)
	response.ResponseOK(c, rollAllVoList)
}
func GetRoleNoPermission(c *gin.Context) {
	permissionId, _ := strconv.Atoi(c.Query("permissionId"))
	roleName := c.Query("roleName")
	roleKey := c.Query("roleKey")
	rollAllVoList := selectRoleByPermissionId(permissionId, roleName, roleKey, 1)
	response.ResponseOK(c, rollAllVoList)

}
func AddRolePermission(c *gin.Context) {
	var rolePermissionDto models.RolePermissionDto
	if err := c.ShouldBindJSON(&rolePermissionDto); err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	roleIds := rolePermissionDto.RoleId
	permissionIds := rolePermissionDto.PermissionId
	models.Db.Model(new(models.RolePermission)).
		Where("role_id in (?)", roleIds).
		Where("permission_id in (?)", permissionIds).
		Delete(new(models.RolePermission))
	for _, roleId := range roleIds {
		for _, permissionId := range permissionIds {
			var rolePermission models.RolePermission
			rolePermission.PermissionId = int(permissionId.Int64)
			rolePermission.RoleId = roleId
			models.Db.Model(new(models.RolePermission)).Create(&rolePermission)
		}
	}
	var userIds []int
	models.Db.Model(new(models.UserRole)).
		Select("user_id").
		Where("role_id in (?)", roleIds).
		Find(&userIds)
	for _, userId := range userIds {
		redisUtil.DelUserPermission(userId)
	}
	response.ResponseOK(c, nil)
	return
}
func DeleteRolePermission(c *gin.Context) {
	var rolePermissionDto models.RolePermissionDto
	if err := c.ShouldBindJSON(&rolePermissionDto); err != nil {
		response.ResponseError(c, HttpCode.FAILURE)
		return
	}
	models.Db.Model(new(models.RolePermission)).
		Where("role_id in (?)", rolePermissionDto.RoleId).
		Where("permission_id in (?)", rolePermissionDto.PermissionId).
		Delete(new(models.RolePermission))
	response.ResponseOK(c, nil)
}
func selectRoleByPermissionId(permissionId int, roleName string, roleKey string, typeOfSelect int) []models.RoleAllVo {
	var rollAllVoList []models.RoleAllVo
	var roleIds []int
	db := models.Db.Model(new(models.RolePermission)).
		Select("role_id").
		Where("permission_id = ?", permissionId)
	if typeOfSelect == 1 {
		var roleIdsYes []int
		db.Find(&roleIdsYes)
		var roleIdsAll []int
		models.Db.Model(new(models.RolePermission)).Select("role_id").Find(&roleIdsAll)
		roleIds = helper.DiffOfIntArray(roleIdsAll, roleIdsYes)

	} else {
		db.Find(&roleIds)
	}
	db2 := models.Db.Model(new(models.RoleAllVo)).Where("id in (?)", roleIds)
	if roleName != "" {
		db2.Where("role_name like ?", "%"+roleName+"%")
	}
	if roleKey != "" {
		db2.Where("role_key like ?", "%"+roleKey+"%")
	}
	db2.Find(&rollAllVoList)
	return rollAllVoList

}
